package com.jrx.anytxn.transaction.job.paph.step;

import com.jrx.anytxn.param.constant.Constant;
import com.jrx.anytxn.transaction.bean.PaphFileBean;
import com.jrx.anytxn.transaction.config.PaphFileConfig;
import com.jrx.anytxn.transaction.constant.TransBizConstant;
import com.jrx.anytxn.transaction.service.IPaphFileService;
import org.springframework.batch.item.ItemWriter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 张靖烽
 * @name GenerateServiceFeeWriter
 * @description 将文件内容写入文件
 * @create 2021-03-13 13:42
 **/
@Service
public class GenerateServiceFeeWriter implements ItemWriter<PaphFileBean> {

    @Autowired
    private PaphFileConfig paphFileConfig;
    @Autowired
    private IPaphFileService paphFileService;

    @Override
    public void write(List<? extends PaphFileBean> items) {
        List<String> xfList = new ArrayList<>();
        List<String> jyList = new ArrayList<>();
        String[] str;
        for (PaphFileBean item : items) {
            str = item.getFileString().split(",");
            if (Constant.PRODECUT_TYPE_1.equals(item.getProductType())) {
                xfList.add(str[0]);
                xfList.add(str[1]);
            } else {
                jyList.add(str[0]);
                jyList.add(str[1]);
            }
        }
        // 生成服务费文件 消费贷和经营贷
        paphFileService.generateFile(xfList, paphFileConfig.getServiceFeePath(), TransBizConstant.SERVICE_FEE_FILE_NAME, paphFileConfig.getFileType(), Constant.PRODECUT_TYPE_1);
        paphFileService.generateFile(jyList, paphFileConfig.getServiceFeePath(), TransBizConstant.SERVICE_FEE_FILE_NAME, paphFileConfig.getFileType(), Constant.PRODECUT_TYPE_6);
    }
}
